/**
 * @(#)ApkConfigController.java 2017年03月1日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.domian.MiguMusicConfigBean;
import org.jsbd.boss.domian.MiguMusicRetentConfigBean;
import org.jsbd.boss.domian.MiguVideoConfigBean;
import org.jsbd.boss.domian.MiguVideoRetentConfigBean;
import org.jsbd.boss.domian.channel.ChannelRetentConfig;
import org.jsbd.boss.service.IMiguMusicConfigService;
import org.jsbd.boss.service.IMiguMusicRetentConfigService;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/miguMusicRetentConfig")
public class MiguMusicRetentConfigController{

	private static final Logger LOGGER=LoggerFactory.getLogger(MiguMusicRetentConfigController.class);
	
	@Autowired
	private IMiguMusicRetentConfigService miguMusicRetentConfigService;
	@Autowired
	private IMiguMusicConfigService miguMusicConfigService;

	/**
	 * 列表页面
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "listModel")
	public String showMiguMusicRetentConfigList(Model model) throws Exception {
		return "/miguMusicRetentConfig/miguMusicRetentConfigList";
	}

	@RequestMapping(value = "/retentList")
	@ResponseBody
	public Map<String, Object> retentPageList(DataGridModel model, String name) {
		Map<String, Object> map = new HashMap<String, Object>(); 
		List<MiguMusicRetentConfigBean> retrntList = new ArrayList<MiguMusicRetentConfigBean>();
		
		if (StringUtils.isBlank(name)) {
			map.put("rows", retrntList);
			return map;
		}
		
		MiguMusicConfigBean queryMvc = new MiguMusicConfigBean();
		queryMvc.setName(name);
		MiguMusicConfigBean miguMusicConfig = miguMusicConfigService.getMiguMusicConfig(queryMvc);
		if (miguMusicConfig == null) {
			map.put("rows", retrntList);
			return map;
		}
		
		
		MiguMusicRetentConfigBean mmrc = new MiguMusicRetentConfigBean();
		mmrc.setName(name);
		retrntList = miguMusicRetentConfigService.getMiguMusicRetentConfigList(mmrc);
		map.put("rows", retrntList);
		return map;
	}
	
	@RequestMapping(value = "/saveRetent")
	public void saveRetent(@RequestBody MiguMusicRetentConfigBean[] list, HttpServletResponse response) {
		List<MiguMusicRetentConfigBean> mvrcs = Arrays.asList(list);
		
		MiguMusicConfigBean queryMvc = new MiguMusicConfigBean();
		queryMvc.setName(mvrcs.get(0).getName());
		MiguMusicConfigBean miguMusicConfig = miguMusicConfigService.getMiguMusicConfig(queryMvc);
		
		for (MiguMusicRetentConfigBean temp : mvrcs) {
			temp.setMiguMusicId(miguMusicConfig.getId());
		}
		miguMusicRetentConfigService.deleteByMiguMusicId(miguMusicConfig.getId());
		miguMusicRetentConfigService.saveRetent(mvrcs);
		ResponseUtils.responseSuccess(response);
	}
	
	@RequestMapping(value = "/batchDelete", method = RequestMethod.POST)
	public void deleteRetentBatch(@RequestBody List<Map> list, HttpServletResponse response) {
		String name = (String) list.get(0).get("name");
		MiguMusicConfigBean queryMvc = new MiguMusicConfigBean();
		queryMvc.setName(name);
		MiguMusicConfigBean miguMusicConfig = miguMusicConfigService.getMiguMusicConfig(queryMvc);
		
		for (Map map : list) {
			MiguMusicRetentConfigBean mvrcb = new MiguMusicRetentConfigBean();
			mvrcb.setRetentRate(Integer.valueOf(String.valueOf(map.get("retentRate"))));
			mvrcb.setRetentDay(Integer.valueOf(String.valueOf(map.get("retentDay"))));
			mvrcb.setMiguMusicId(miguMusicConfig.getId());
			miguMusicRetentConfigService.deleteRetentBatch(mvrcb);
		}
		ResponseUtils.responseSuccess(response);
	}
	
	@RequestMapping(value = "copyMiguMusicRetentConfig")
	@ResponseBody
	public Object copyMiguMusicRetentConfig(String name){
		if(name == null){
			return ResponseMapUtils.responseFail("参数缺失");
		}
		String[] nameArr = name.split(" ");
		String sourceName = nameArr[0];
		String targetName = nameArr[1];
		
		MiguMusicConfigBean queryMmcb = new MiguMusicConfigBean();
		queryMmcb.setName(sourceName);
		MiguMusicConfigBean sourceMmcb = miguMusicConfigService.getMiguMusicConfig(queryMmcb);
		List<MiguMusicRetentConfigBean> sourcelist = miguMusicRetentConfigService.getByMiguMusicId(sourceMmcb.getId());
		
		queryMmcb.setName(targetName);
		MiguMusicConfigBean targetMmcb = miguMusicConfigService.getMiguMusicConfig(queryMmcb);
		miguMusicRetentConfigService.deleteByMiguMusicId(targetMmcb.getId());
		List<MiguMusicRetentConfigBean> targetList = new ArrayList<MiguMusicRetentConfigBean>();
		for (MiguMusicRetentConfigBean sourceBean : sourcelist) {
			MiguMusicRetentConfigBean temp = new MiguMusicRetentConfigBean();
			temp.setMiguMusicId(targetMmcb.getId());
			temp.setRetentDay(sourceBean.getRetentDay());
			temp.setRetentRate(sourceBean.getRetentRate());
			targetList.add(temp);
		}
		miguMusicRetentConfigService.copyMiguMusicRetentConfig(targetList);
		
		return ResponseMapUtils.responseSuccess();
	}

}
